<?php
//购物车类,本购物车使用cookie，不用session是因为session比较占服务器资源
class Cart {
    public function showProduct() {
        $myCart = array();
        if(isset($_COOKIE['cart'])) {
            foreach($_COOKIE['cart'] as $key=>$value) {
                $myCart[$key] = unserialize($value);
                $myCart[$key]['total_small'] = round($myCart[$key]['price_sale'] * $myCart[$key]['num'],2);  //获得小计
            }
        }
        return $myCart;
    }

    public function getTotalCost() {
        $cart = $this->showProduct();
        $total = 0;
        if(isset($cart)) {
            foreach($cart as $value) {
                $total += $value['total_small'];
            }
        }
        return $total;
    }

    public function addProduct() {
        setcookie('prev',$_POST['prev']);
        $attrStr = substr($_POST['attr'],0,-1);
        $t = explode(';',$attrStr);
        $newt = array();
        $i = 0;
        foreach($t as $key=>$value) {
            $temp1 = explode(':',$value);
            if(!array_key_exists($temp1[0],$newt)) $i=0;      //检验是否有相同的key，如果没有，则说明创建一个新数组，则下标必须从0开始
            $newt[$temp1[0]][$i] = $temp1[1];
            $i++;
        }
        if(!isset($_COOKIE['cart'][$_POST['id']])) {
            //把商品id作为唯一标示的key值，可以防止商品被覆盖
            //cookie不能使用数组作为value，因此要序列化数组才能存储
            return setcookie('cart['.$_POST['id'].']',
                       serialize(array(
                                        'id'=>$_POST['id'],
                                        'name'=>$_POST['name'],
                                        'attr'=>$newt,
                                        'sn'=>$_POST['sn'],
                                        'num'=>$_POST['num'],
                                        'price_sale'=>$_POST['price_sale'],
                                        'weight'=>$_POST['weight'],
                                        'nav'=>$_POST['nav'],
                                        'thumbnail_small'=>$_POST['thumbnail_small'],
                                        'total_small'=>$_POST['num'] * $_POST['price_sale']
                                      ))
            ,time() + 60 * 60 * 24 * 7);
        } else {
            $carArr = array();
            $carArr = unserialize($_COOKIE['cart'][$_POST['id']]);
            $carArr['num'] = $carArr['num'] + $_POST['num'];
            return setcookie('cart['.$_POST['id'].']',serialize($carArr),time() + 60 * 60 * 24 * 7);
        }
    }

    public function delPro() {
        if(isset($_COOKIE['cart'][$_GET['id']])) {
            return setcookie('cart['.$_GET['id'].']','',time()-1);
        }
    }

    public function clearCar() {
        if(isset($_COOKIE['cart'])) {
            foreach($_COOKIE['cart'] as $key=>$value) {
                setcookie('cart['.$key.']','',time()-1);
            }
        }
        return true;
    }

    public function changeNum() {
        $carArr = array();
        $carArr = unserialize($_COOKIE['cart'][$_POST['id']]);
        $carArr['num'] = $_POST['num'];
        $carArr['total_small'] = $_POST['num'] * $_POST['price'];
        setcookie('cart['.$_POST['id'].']',serialize($carArr),time() + 60 * 60 * 24 * 7);
        $total = 0;
        foreach($_COOKIE['cart'] as $key=>$value) {
            if($key == $_POST['id']) {
                $total += $carArr['total_small'];
            } else {
                $cart = unserialize($value);
                $total += $value['num'];
            }
        }
        echo $carArr['total_small'];
    }

}

?>